package com.person.consumerhystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.person.common.CommonResult;
import com.person.consumerhystrix.feignclient.PaymentHystrixFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_timeoutGlobalHandler", commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
})
public class PaymentHystrixController {

    @Resource
    PaymentHystrixFeignClient paymentHystrixFeignClient;

    @GetMapping("/consumer80/time/ok/{id}")
    public CommonResult ok(@PathVariable("id") Integer id) {
        log.info("requst***********ok" + id);
        return paymentHystrixFeignClient.payment_ok(id);
    }


    @HystrixCommand(fallbackMethod = "payment_timeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    //@HystrixCommand  update
    //这里是我的冲突
    @GetMapping("/consumer80/time/timeout/{id}")
    public CommonResult timeout(@PathVariable("id") Integer id) {
        log.info("request************timeout" + id);
        int ss = 10 / 3;
        return paymentHystrixFeignClient.payment_timeout(id);
    }

    public CommonResult payment_timeoutHandler(@PathVariable("id") Integer id) {
        return CommonResult.error("error", "80服务器繁忙或出错请稍后重试" + "\t" + "o(╥﹏╥)o");
    }


    public CommonResult payment_timeoutGlobalHandler() {
        return CommonResult.error("error", "程序出错了o(╥﹏╥)o,小哥哥正在抢修,请耐心等待!!!");
    }
}
